//
//  FTextInput.swift
//  takeaway
//
//  Created by zhuxietong on 2021/2/21.
//

import Foundation

open class FTextInput: FText {
    open override func willLayoutRules() {
        super.willLayoutRules()
        textField.delegate = self
        bk.bsui.background(color: ._tableBackground).set(radius: 3, width: 1, color: UIColor._separator.alpha(0.3))
    }
    public let bk = ASDisplayNode()
    open override func didLoadProps() {
        textField.text = "\(value)"
        lableNode.attributedText = _lable
        textField.font = valueStyle.font
        textField.textColor = valueStyle.color
        textField.attributedPlaceholder = placeholder.ui.font(font: placeHolderStyle.font).color(color: placeHolderStyle.color).left.astring
    }
    
    open override func specGetter() -> _Spec {
        return .stack(["v|8",&-.stretch,_nowrap], childs: [
            lableNode.Spec,
            _Spec.inset([], [3,8], child: node["28"]).addBack(bk.Spec)
//            node["36"]
        ])
    }
}



open class FPriceInput:FTextInput{
 
    
    public let uniL = ASTextNode2()

    open override var _lable:NSAttributedString{
        get{
       
            return label.ui.font(font: labelStyle.font).color(color: labelStyle.color).astring
        }
    }
    
    
    open override func addLayoutRules() {
        super.addLayoutRules()
        valueStyle = [.font(.systemFont(ofSize: 39.fs, weight: .bold)),.color(._foreground)]
        placeHolderStyle = [.color(.clear),.font(.systemFont(ofSize: 39.fs, weight: .bold))]
        textField.delegate = self
        textField.keyboardType = .decimalPad
//            textField.setPlaceholder(info: ("",UIColor.color(light: "#2223", dark: "#fff3"),UIFont.systemFont(ofSize: 39.fs)))
        view.backgroundColor = .clear
        bk.backgroundColor = ._tableBackground
        bk.bsui.set(radius: .radius, width: nil, color: nil)
        uniL.attributedText = "¥".ui.font(font: .systemFont(ofSize: 34.fs)).color(color: ._foreground).astring
        

        
    }
    open override func mo_model_reload() {
        
     
        
    }
    
    
    
    @objc func begenPay(){
        textField.resignFirstResponder()
        let text = textField.text ?? "0"
        if let _ = Regex("(^[1-9]\\d*(\\.\\d{1,2})?$)|(^0(\\.\\d{1,2})?$)").firstMatch(in: text){
            if let p = Double(text){
                if(p > 0){
//                        "支付金额:\(p) ¥".alert()
                    return
                }
            }
            
        }
        "请输入有效的金额".alert()
        
    }

    @objc func endEdit(){
        textField.resignFirstResponder()
    }
    
    
    open override func specGetter() -> _Spec {
        return .inset([], .zero, child: .stack(["v|\(12.fs)",&-.stretch], childs: [
            lableNode.Spec,
            _Spec.inset([], [14.fs,21.fs], child: .stack(["h|\(10.fs)",&-.center], childs: [
                uniL.Spec,
                node["\(60.fs)",_grow(1),_shrink(1)]
            ])).addBack(bk.Spec)
        ]))
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        let text = textField.text ?? ""
        if text.hasSuffix("."){
            textField.text = text.replacingOccurrences(of: ".", with: "")
        }
    }
    open func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        var text = textField.text ?? ""
        if text.contains("."){
            if string.contains("."){
                return false
            }
        }
        
        if text == "0"{
            if("0123456789".contains(string)){
                return false
            }
        }
        if text.len < 1{
            if string.contains("."){
                return false
            }
        }
        if let _ = Regex("\\d+\\.\\d{2,10}$").firstMatch(in: text){
            if("0123456789".contains(string)){
                return false
            }
        }
        if text.hasSuffix("."){
            text = "\(text).0"
        }
        if let v = Double(text){
            if v > 300000{
                if("0123456789".contains(string)){
                    return false
                }
            }
        }
//            let r = Regex("(^[1-9]\\d*(\\.\\d{1,2})?$)|(^0(\\.\\d{1,2})?$)")
//            if let result = r.firstMatch(in: text){
//
//            }
        return true
    }
    
    
}
